﻿package net.area80.utils {
	
	/**
	 * Color
	 * @author Wissarut Pimanmassuriya
	 */
	public class Color {
		private var _r:Number = 1;
		private var _g:Number = 1;
		private var _b:Number = 1;
		
		public function get r ():Number { return Math.min(255,Math.max(1,_r)); }
		public function set r (n:Number):void { _r =  Math.min(255, Math.max(1, n)); }
		public function get g ():Number { return Math.min(255,Math.max(1,_g)); }
		public function set g (n:Number):void { _g =  Math.min(255, Math.max(1, n)); }
		public function get b ():Number { return Math.min(255,Math.max(1,_b)); }
		public function set b (n:Number):void { _b =  Math.min(255,Math.max(1,n)); }
		
		public function Color (n:Number):void {
				value = n;
		}
		public function get value ():Number {
				return (r << 16 | g << 8 | b);
		}
		public function set value (n:Number):void {
				createFromNumber(n);
		}
		
		private function createFromNumber (n:Number):void {
				r = (n >> 16 & 0xFF);
				g = (n >> 8 & 0xFF);
				b = (n & 0xFF);
		}
		public static function RGBToNumber (r:Number, g:Number, b:Number):Number {
				return (r << 16 | g << 8 | b); //Translates RGB to HEX;
		}
		public static function numberToHexString (number: Number):String {
				// A hexit is the base-16 equivelant of a digit. 
				var hexit:uint = 0;
                // The string that will be output at the end of the function.
                var string:String = "";
                var minimumLength:Number = 6;
                // otherwise, loop through all hexits in the number
                while (number > 0 || minimumLength > 0) {
                        minimumLength--;
                        
                        // get the first hexit from the number by masking the rest
                        hexit = number & 0xF;
                        // convert the digits over 9 into letters
                        switch(hexit) {
                                case 0xA: string = "A" + string; break;
                                case 0xB: string = "B" + string; break;
                                case 0xC: string = "C" + string; break;
                                case 0xD: string = "D" + string; break;
                                case 0xE: string = "E" + string; break;
                                case 0xF: string = "F" + string; break;
                                default: string = hexit.toString() + string;
                        }
                        // erase the hexit from the original number
                        number >>>= 4;
                }
                
                return "0x" + string;
		}

		public static function hexStringToNumber(hexStr:String):Number {
			if( hexStr.charAt(0) == "#" && hexStr.length > 7 ) {
				return Number("*"); // NaN;
			}
			if( hexStr.charAt(0) != "#" && hexStr.length > 6 ){
				return Number("*"); // NaN;
			}
			var newStr:String;
			if( hexStr.charAt(0) == "#" ) {
				newStr = hexStr.substr(1, hexStr.length);
			} else {
				newStr = hexStr;
			}
			
			if( newStr.length < 6 ) {
				var z:String = "000000";
				// add zeros to the string to make it 6 characters long
				newStr = newStr + z.substr(0, z.length - newStr.length); 
			}
			var numStr:String = "0x" + newStr;
			var num:Number = Number(numStr);
			return num;
		}

	}
	
}